<template>
  <div id="app">
    <router-view />
  </div>
</template>
<script>
import store from "./store";
import axios from "axios";
import API from "@/services";
export default {
  data() {
    return {
      token: "",
    };
  },
  created() {
    //定义测试的token
    let testToken;
    //这个appToken是截取的APP传过来的token
    const appToken = (
      (decodeURI(window.location.href).split("?")[1] || "")
        .split("&")
        .find((item) => item.indexOf("token") > -1) || ""
    ).split("token=")[1];
    const localToken = localStorage.getItem("token");
    // testToken = "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl9tZW1iZXJfa2V5IjoiNDM3MzNmMTctNDU3Mi00ZGM5LTk2OTMtMWY1OGQwY2MzZTA5In0.HqzjQTDl6Zpf-wimuAn5u5cMPTHJ0gqEctag5ZTUKqPI2fywHjtqIMzqN3i1HypepJwiVEp26a4nY6Y3BZX2cQ"

    //判断之前的是否有APP传过来的token
    if (appToken == undefined || appToken == null || appToken == "") {
      this.token = testToken;
    } else {
      this.token = appToken;
    }
    if (!appToken && localToken) {
      store.commit("setToken", localToken);
    }

    store.commit("setAppToken", appToken);

    this.getToken();
  },
  methods: {
    //请求新的token
    async getToken() {
      let param = new URLSearchParams();
      param.append("xSugarToken", this.token);
      param.append("type", 1);
      const res = await API.getNewToken({
        data: param,
      });
      this.token = res.token;
      store.commit("setToken", this.token);
      store.commit("setRole", res.userInfo.role);
      //this.getRole() //执行下面定义的获取角色的请求
    },
    async getRole() {
      const res = await API.getRole({});
      if (res !== "") {
        store.commit("setRole", res);
      }
    },
  },
};
</script>
<style lang="scss">
@import "./assets/common.scss";

@font-face {
  font-family: currentFont;
  src: url("./assets/font/PingFang-SC-Regular.ttf");
}

html,
body {
  width: 100%;
  height: 100%;
  font-weight: normal;
  font-family: currentFont;
  color: #333333;
  font-size: 14px;
}

* {
  box-sizing: border-box;
}

#app {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  width: 100%;
  height: 100%;
}
</style>
